package com.aim.controller.system;

import com.aim.common.annotion.Permission;
import com.aim.common.base.AjaxResult;
import com.aim.common.entity.CheckArr;
import com.aim.common.entity.ZTreeNode;
import com.aim.common.service.MenuNodeService;
import com.aim.common.service.ZtreeNodeService;
import com.aim.common.util.BeanUtils;
import com.aim.common.util.ConverterUtils;
import com.aim.common.util.OftenUtil;
import com.aim.config.shiro.ShiroKit;
import com.aim.entity.system.SysMenu;
import com.aim.service.system.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @AUTO 菜单控制器
 * @Author AIM
 * @DATE 2019/4/23
 */
@Controller
@RequestMapping("/menu")
public class SysMenuController {

	private static String PREFIX = "system/menu/";

	@Autowired
	private SysMenuService menuService;
	@Autowired
	private MenuNodeService nodeService;
	@Autowired
	private ZtreeNodeService ztreeNodeService;

	@GetMapping("")
	public String index() {
		return PREFIX + "list.html";
	}

	/**
	 * 查询列表
	 */
	@PostMapping("/list")
	@Permission
	@ResponseBody
	public Object list(String menuName, String level) {
		List<Map<String, Object>> menuTreeList = nodeService.getMenuTreeList(menuName, level);
		return AjaxResult.success(menuTreeList);
	}

	/**
	 * 添加编辑页面
	 */
	@Permission
	@RequestMapping(value = {"add", "update"})
	public String editView(String id, Model model) {
		if (OftenUtil.isNotEmpty(id)) {
			SysMenu sysMenu = menuService.get(ConverterUtils.toLong(id));
			SysMenu pmenu = new SysMenu();
			pmenu.setCode(sysMenu.getPcode());
			pmenu = menuService.get(pmenu);
			model.addAttribute("menu", sysMenu);
			model.addAttribute("pid", pmenu == null ? "0" : pmenu.getMenuId());
		}
		return PREFIX + "edit.html";
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
	@ResponseBody
	public Object save(SysMenu menu, String pid) {
		if (OftenUtil.isNotEmpty(pid)) {
			SysMenu pmenu = this.menuService.get(ConverterUtils.toLong(pid));
			menu.setPcode(pmenu == null ? "0" : pmenu.getCode());
			menu.setLevels(pmenu == null ? 1 : pmenu.getLevels() + 1);
		} else {
			menu.setPcode("0");
			menu.setLevels(1);
		}

		SysMenu sysMenu = this.menuService.get(menu.getMenuId());
		if (null == sysMenu) {
			menu.setCreateTime(new Date());
			menu.setCreateUser(ShiroKit.getUser().getUserId());
			this.menuService.save(menu);
		} else {
			BeanUtils.copyProperties(menu, sysMenu);
			sysMenu.setUpdateTime(new Date());
			sysMenu.setUpdateUser(ShiroKit.getUser().getUserId());
			this.menuService.update(sysMenu);
		}
		return AjaxResult.success();
	}

	/**
	 * 删除
	 *
	 * @param ids 删除ID集合
	 */
	@Permission
	@PostMapping("/del")
	@ResponseBody
	public Object del(String ids) {
		if (OftenUtil.isNotEmpty(ids)) {
			this.menuService.batchDelete(ids.split(","));
			return AjaxResult.success();
		} else {
			return AjaxResult.error("参数异常");
		}
	}

	/**
	 * 获取栏目的tree列表
	 */
	@RequestMapping(value = "/tree")
	@ResponseBody
	public Object tree() {
		List<ZTreeNode> tree = this.ztreeNodeService.menuZtree();
		tree.add(ZTreeNode.createParent());
		for (ZTreeNode zTreeNode : tree) {
			List<CheckArr> checkArr = new ArrayList<CheckArr>();
			checkArr.add(new CheckArr());
			zTreeNode.setCheckArr(checkArr);
		}
		return AjaxResult.success(tree);
	}
}
